<template>
  <div>
    <el-card class="update-log msg-table-list" style="height: 366px;">
      <div slot="header" class="clearfix">
        <span>通知内容（<span class="badge-arrow-num">{{this.total}}</span>）</span>
        <span class="header-more-title">
          <template >
            <router-link :to="'/platform/msg'" class="link-type">
              <span>更多<i class="el-icon-arrow-right"></i></span>
            </router-link>
          </template>
        </span>
      </div>
      <div class="body">
        <el-table v-loading="loading" :data="msgList" @row-click="handleUpdate" style="width: 100%;" :show-header="false">
          <template >
            <el-table-column prop="msgTitle" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column width="95px" prop="createTime">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.creationDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
          </template>
        </el-table>
      </div>
    </el-card>

     <!-- 处理消息对话框 -->
     <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
        <el-form ref="form" :model="form" label-width="80px">
          <el-row>
            <el-col :span="24">
              <!-- <el-form-item label="内容："> -->
                <div v-text="form.msgContent"></div>
              <!-- </el-form-item> -->
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">已 读</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <el-dialog :title="title" v-if="linkOpen" :visible.sync="linkOpen" width="800px" append-to-body>
        <msg-url ref="task" :link="link" @cancelMsg="cancelMsg"/>
      </el-dialog>

  </div>

</template>

<script>
import { listMsg, getMsgById, handlerMsg } from "@/api/common/msg";
import MsgUrl from "@/views/dashboard/components/MsgUrl";

  export default {
    components: {
      MsgUrl
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 未处理消息列表
        msgList: [],
        form: {
          msgContent: ''
        },
        link: '',
        //消息总数
        total: 0,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        linkOpen: false,
        // 查询参数
        queryParams: {
          pageParameter: { page: 1, rows: 7 },
          searchParams: {
            msgStatus: '0'
          }
        },
      }
    },
    created() {
      //初始化消息列表
      this.initList();
    },
    methods: {
      //初始化未处理消息列表
      initList() {
        this.loading = true;
        listMsg(this.queryParams).then(response => {
          this.msgList = response.responseBody.result;
          this.total = response.responseBody.pageParameter.totalCount;
          this.loading = false;
        })
      },
      /** 查询详情修改操作 */
      handleUpdate(row) {
        if (row.msgUrl) {
          this.openMsgUrl(row.msgUrl, row.msgTitle);
        } else {
          const msgId = row.msgId
          getMsgById(msgId).then(response => {
            this.form = response.responseBody;
            this.open = true;
            this.title = this.form.msgTitle;
          });
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      /** 已读处理 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.msgId != undefined) {
              handlerMsg({msgId:this.form.msgId, msgStatus: '1'}).then(response => {
                this.msgSuccess("已处理！");
                this.open = false;
                this.initList();
              });
            }
          }
        });
      },
      /** 外链处理 */
      openMsgUrl(msgUrl, msgTitle) {
        this.title = msgTitle;
        this.link = msgUrl;
        this.linkOpen = true;
      },
      cancelMsg() {
        this.linkOpen = false;
      }
    }
  }
</script>

<style lang="scss">
  .el-table tr{
    cursor: pointer;
  }
</style>

